package com.ihrm.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ihrm.common.utils.PageUtils;
import com.ihrm.common.utils.Query;

import com.ihrm.modules.sys.dao.PermissionDao;
import com.ihrm.modules.sys.entity.PermissionEntity;
import com.ihrm.modules.sys.service.PermissionService;


@Service("permissionService")
public class PermissionServiceImpl extends ServiceImpl<PermissionDao, PermissionEntity> implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PermissionEntity> page = this.page(
                new Query<PermissionEntity>().getPage(params),
                new QueryWrapper<PermissionEntity>()
        );

        return new PageUtils(page);
    }


    // 树形结构生成
    // 1、递归
    public List<PermissionEntity> queryLevelPermsRecursive(Map<String, Object> params) {
        Integer type = null;
        if (params.containsKey("type")){
            type = (Integer) params.get("type");
        }
        List<PermissionEntity> list = new LambdaQueryChainWrapper<>(permissionDao)
                .le(type != null, PermissionEntity::getType, type)
                .list();
        if(CollectionUtils.isEmpty(list)) {
            return null;
        }
        PermissionEntity permissionEntity = new PermissionEntity();
        permissionEntity.setPid("f");
        permissionEntity.setId("0");
        levelPermsRecursive(permissionEntity, list);
        return permissionEntity.getChildren();
    }
    private void levelPermsRecursive(PermissionEntity vo, List<PermissionEntity> districtEntityVoList) {
        // 1、如果是指定层级或者集合为空结束递归调用
        if ( districtEntityVoList == null || districtEntityVoList.size() == 0) {
            return;
        }
        // 2、遍历查找集合中pid和目标id相同的元素
        List<PermissionEntity> children = new LinkedList<>();
        Iterator<PermissionEntity> iterator = districtEntityVoList.iterator();
        while (iterator.hasNext()) {
            PermissionEntity next = iterator.next();
            if (StringUtils.equals(vo.getId(), next.getPid())) {
                // 2.1、找到元素，加入子集合；同时删除在原有集合中移除
                children.add(next);
                iterator.remove();
            }
        }
        if (children.size() > 0) {
            // 3、子集合不为空，递归封装层级数据
            vo.setChildren(children);
            for (PermissionEntity child: children) {
                levelPermsRecursive(child, districtEntityVoList);
            }
        }
    }
    // 2、队列

    public List<PermissionEntity> levelPermsQueue(Map<String, Object> params) {
//        long start = System.currentTimeMillis();
        Integer type = null;
        if (params.containsKey("type")){
            type = (Integer) params.get("type");
        }
        List<PermissionEntity> list = new LambdaQueryChainWrapper<>(permissionDao)
                .le(type != null, PermissionEntity::getType, type)
                .list();
        if(CollectionUtils.isEmpty(list)) {
            return null;
        }
        PermissionEntity pe = new PermissionEntity();
        pe.setPid("f");
        pe.setId("0");
        LinkedList<PermissionEntity> queue = new LinkedList<>();
        queue.addLast(pe);


        while (CollectionUtils.isNotEmpty(queue) && CollectionUtils.isNotEmpty(list)) {

            PermissionEntity p = queue.removeFirst();

            Iterator<PermissionEntity> iterator = list.iterator();
            LinkedList<PermissionEntity> children = new LinkedList<>();
            while (iterator.hasNext()) {
                PermissionEntity next = iterator.next();
                if (StringUtils.equals(p.getId(), next.getPid())){
                    children.add(next);
                    iterator.remove();

                }
            }
            if (children.size() > 0) {
                p.setChildren(children);
                queue.addAll(children);
            }
        }
//        long end = System.currentTimeMillis();
//        System.out.println(end - start);
        return pe.getChildren();
    }

    @Override
    public List<String> queryByRoleId(String id) {
        return permissionDao.queryByRoleId(id);
    }

    // 3栈结构
    @Override
    public List<PermissionEntity> levelPermsStack(Map<String, Object> params) {
//        long start = System.currentTimeMillis();
        Integer type = null;
        if (params.containsKey("type")){
            type = (Integer) params.get("type");
        }
        List<PermissionEntity> list = new LambdaQueryChainWrapper<>(permissionDao)
                .le(type != null, PermissionEntity::getType, type)
                .list();
        if(CollectionUtils.isEmpty(list)) {
            return null;
        }
        PermissionEntity permissionEntity = new PermissionEntity();
        permissionEntity.setPid("f");
        permissionEntity.setId("0");
        LinkedList<PermissionEntity> stack = new LinkedList<>();
        stack.addLast(permissionEntity);

        while (!stack.isEmpty() && list.size() > 0) {
            PermissionEntity m = stack.pop();
            List<PermissionEntity> children = new LinkedList<>();
            Iterator<PermissionEntity> iterator = list.iterator();
            while (iterator.hasNext()) {
                PermissionEntity next = iterator.next();
                if (StringUtils.equals(m.getId(), next.getPid())) {
                    // 2.1、找到元素，加入子集合；同时删除在原有集合中移除
//                    next.setParent(m);
                    children.add(next);
                    iterator.remove();
                }
            }
            if (children.size() > 0) {
                m.setChildren(children);
                stack.addAll(children);
            }
        }
//        long end = System.currentTimeMillis();
//        System.out.println(end - start);
        return permissionEntity.getChildren();
    }

}
